<template>
  <div v-loading="loading" class="list-table">
    <h2 style="text-align: center">{{ $t('基金设立审核要点') }}</h2>
    <!--    <div style="float:right; margin-bottom: 20px;">-->
    <!--      <el-button v-hasPermi="['fund:mainpoints:edit']" type="primary" @click="submitForm">{{ $t("保存") }}</el-button>-->
    <!--    </div>-->
    <el-form
      ref="form"
      :model="points"
    >
      <el-table tooltip-effect="darkTable" :data="points.fundKeyPointList" border :span-method="objectSpanMethod" class="formTable noneHoverBg">
        <el-table-column :label="$t('具体分项内容')" align="center" prop="fundMainPointsTemplate.mainPointsCategory" :formatter="mainPointsCategoryFormat" show-overflow-tooltip width="150" />
        <el-table-column :label="$t('是否必要')" align="center" prop="fundMainPointsTemplate.required" :formatter="requiredFormat" width="100" />
        <el-table-column :label="$t('内容')" prop="fundMainPointsTemplate.mainPointsName" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ $t(scope.row.fundMainPointsTemplate.mainPointsName) }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('落实情况')" align="center" prop="implementation" width="260">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('落实情况') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'fundKeyPointList.' + scope.$index + '.implementation'" :rules="rules.implementation" style="margin-bottom:0;display: inline-block;">
              <el-radio-group v-model="scope.row.implementation" size="mini" class="radioGroups">
                <el-radio-button
                  v-for="(dict, index) in implementationOptions"
                  :key="index"
                  :label="dict.dictValue"
                >{{ $t(dict.dictLabel) }}</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('备注')" align="center" prop="remark">
          <template slot-scope="scope">
            <el-input v-model="scope.row.remark" type="textarea" maxlength="2000" :autosize="{ minRows: 2 }" />
          </template>
        </el-table-column>
      </el-table>
      <FileTable :list.sync="points.fileList" data-type="fund_key_point" :business-id="autoBusinessId($route.query.id, form)" />

    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: center;margin-top: 20px">
      <el-button v-hasPermi="['fund:mainpoints:edit']" type="primary" @click="submitForm">{{ $t("保存") }}</el-button>
    </div>
  </div>
</template>

<script>
  import { init, savePoint, list } from '@/api/fund/mainpointstemplate/fundMainPointsTemplateCount'
    export default {
      data() {
        return {
          // 数据
          points: {},
          // 设立要点分类字典
          mainPointsCategoryOptions: [],
          // 是否必要字典
          requiredOptions: [],
          // 完成情况字典
          implementationOptions: [],
          // 蒙层
          loading: false,
          rules: {
            implementation: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }]
          }
        }
      },
      created() {
        this.getList()
        this.getDicts('fund_main_points').then(response => {
          this.mainPointsCategoryOptions = response.data
        })
        this.getDicts('sys_yes_no').then(response => {
          this.requiredOptions = response.data
        })
        this.getDicts('implementation').then(response => {
          this.implementationOptions = response.data
        })
      },
      methods: {
        getList() {
          this.loading = true
          const params = {
            fundId: this.$route.query.id,
            implementation: 'not'
          }
          list(params).then(response => {
            // 查询是否存在设立要点 有就直接赋值 没有就初始化
            if (response.data.fundId !== null) {
              this.points = response.data
              this.loading = false
            } else {
              init().then(response => {
                this.points = response.data
                this.points.fundId = this.$route.query.id
                this.points.fileList = []
                this.loading = false
              })
            }
          })
        },
        // 设立要点分类字典翻译
        mainPointsCategoryFormat(row) {
          return this.selectDictLabel(this.mainPointsCategoryOptions, row.fundMainPointsTemplate.mainPointsCategory)
        },
        // 是否必要字典翻译
        requiredFormat(row) {
          return this.selectDictLabel(this.requiredOptions, row.fundMainPointsTemplate.required)
        },
        objectSpanMethod({ rowIndex, columnIndex }) {
          if (columnIndex === 0) {
            const spanArr = this.rowspan(this.points.fundKeyPointList)
            for (let i = 0; i < spanArr.length; i++) {
              const _row = spanArr[rowIndex]
              return {
                rowspan: _row,
                colspan: 1
              }
            }
          }
        },
        rowspan(data) {
          // 二维数组，用于存放单元格合并规则
          const spanArr = []
          // 用于存储相同项的开始index
          let position = 0
          data.forEach((item, index) => {
            if (index === 0) {
              spanArr.push(1)
              position = 0
            } else {
              if (data[index].fundMainPointsTemplate.mainPointsCategory === data[index - 1].fundMainPointsTemplate.mainPointsCategory) {
                spanArr[position] += 1 // 有相同项
                spanArr.push(0) // 名称相同后往数组里面加一项0
              } else {
                spanArr.push(1) // 同列的前后两行单元格不相同
                position = index
              }
            }
          })
          return spanArr
        },
        /** 提交按钮 */
        submitForm: function() {
          this.$refs['form'].validate(valid => {
            if (valid) {
              this.loading = true
              savePoint(this.points).then(response => {
                if (response.code === 200) {
                  this.msgSuccess(this.$t('保存成功'))
                } else {
                  this.msgError(response.msg)
                }
                this.loading = false
              }).catch(() => {
                this.loading = false
              })
            }
          })
        }
      }
    }
</script>

<style scoped lang="scss">
.radioGroups{
  width: 100%;
  label{
    float: left;
    margin-bottom: 2px;
    margin-top: 2px;
    display: flex;
  }
}
</style>
